﻿using KPI.Api;
using KPI.Entity;
using KPI.UIBase;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TYBase;
using WPF_TY_UI;

namespace CssdAssistantClient.Views
{
    public partial class ViewPublishNaire : TYWindow
    {
        ViewPublishNaireModel vm = new ViewPublishNaireModel();
        private List<OfficeInfoVO> officeData;
        private readonly string examId;

        public ViewPublishNaire(string examId)
        {
            InitializeComponent();
            this.DataContext = vm;
            initCbx();
            this.Loaded += ViewSendNaire_Loaded;
            this.examId = examId;
        }

        private void initCbx()
        {
            cbx.Loaded += delegate
            {
                var editableTextBox = cbx.Template.FindName("PART_EditableTextBox", cbx) as TextBox;
                Popup popup = cbx.Template.FindName("PART_Popup", cbx) as Popup;
                if (editableTextBox != null)
                {
                    editableTextBox.TextChanged += (object sender, TextChangedEventArgs e) =>
                    {
                        var ICV = cbx.ItemsSource as ObservableCollection<StringStringData>;

                        if (ICV != null)
                        {
                            if (string.IsNullOrEmpty(editableTextBox.Text.Trim()))
                                cbx.Items.Filter = null;
                            else
                            {
                                cbx.Items.Filter = new Predicate<object>(i =>
                                {
                                    if (vm.ListOfficeInfoAdd.Count(a => a.Key.NotNull() && a.Key == ((StringStringData)i).Key) > 0)
                                    {
                                        return false;
                                    }
                                    return ((StringStringData)i).Value.Contains(editableTextBox.Text);
                                });


                                cbx.IsDropDownOpen = true;
                            }
                        }
                    };

                }
            };

        }


        private async void ViewSendNaire_Loaded(object sender, RoutedEventArgs e)
        {

            ExamOfficeQuery dto = new ExamOfficeQuery();
            var rData = await Run(() => HospitalInfoApi.GetOfficeList(dto));
            vm.ListOfficeInfo.Clear();
            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            vm.ListOfficeInfo.Add(new StringStringData { Key = null, Value = "全部科室" });
            if (rData.data != null)
            {
                this.officeData = rData.data;
                rData.data.ForEach(x =>
                {
                    vm.ListOfficeInfo.Add(new StringStringData { Key = x.officeCode, Value = x.officeName });
                });
            }
            initCbx();
        }

        private async void BtnClick_Send(object sender, RoutedEventArgs e)
        {
            PublishExamInfo dto = new PublishExamInfo()
            {
                examId = examId,
                listOfficeInfo = new List<PublishOfficeInfo>(),
            };

            if (vm.ListOfficeInfoAdd.Count(x => x.Key.Null()) > 0)
            {
                foreach (var item in vm.ListOfficeInfo)
                {
                    if (item.Key.NotNull())
                        dto.listOfficeInfo.Add(new PublishOfficeInfo { examId = examId, officeCode = item.Key, needCount = vm.NeedCount });
                }
            }
            else
            {
                foreach (var item in vm.ListOfficeInfoAdd)
                {
                    dto.listOfficeInfo.Add(new PublishOfficeInfo { examId = examId, officeCode = item.Key, needCount = vm.NeedCount });
                }
            }
            if (dto.listOfficeInfo.Count == 0)
            {
                MessageBox.Show("请选择科室!");
                return;
            }
            var rData = await Run(() => QuesionNaireApi.PublishExam(dto));

            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            if (rData.data != null)
            {
                this.DialogResult = true;
            }
        }

        private void BtnClick_Add(object sender, RoutedEventArgs e)
        {
            if (vm.SelectedOfficeInfo != null)
            {
                if (vm.SelectedOfficeInfo.Key.Null())
                {
                    vm.ListOfficeInfoAdd.Clear();
                    vm.ListOfficeInfoAdd.Add(vm.SelectedOfficeInfo);
                    btnAdd.IsEnabled = false;
                }
                else
                {
                    vm.ListOfficeInfoAdd.Add(vm.SelectedOfficeInfo);
                    vm.ListOfficeInfo.Remove(vm.SelectedOfficeInfo);
                }
            }
        }

        private void BtnClick_DeleteFromAdd(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as StringStringData;
            if (dx != null)
            {
                if (dx.Key.Null())
                {

                    vm.ListOfficeInfoAdd.Remove(dx);
                }
                else
                {
                    vm.ListOfficeInfo.Insert(0, dx);
                    vm.ListOfficeInfoAdd.Remove(dx);
                }
                btnAdd.IsEnabled = true;
            }
        }

    }
    //ExamUserApi
}
